package com.yc.music.manager;

import ohos.media.common.AVMetadata;
import ohos.media.common.sessioncore.AVPlaybackState;
import ohos.media.common.sessioncore.AVSessionCallback;
import ohos.media.sessioncore.AVSession;

import com.yc.music.model.AudioBean;
import com.yc.music.service.PlayService;
import com.yc.music.tool.BaseAppHelper;

/**
 * MediaSessionManager
 *
 * @since 2021-05-10
 */
public class MediaSessionManager {
    private static final String TAG = "MediaSessionManager";
    private static final long MEDIA_SESSION_ACTIONS = AVPlaybackState.PLAYBACK_ACTION_PLAY
        | AVPlaybackState.PLAYBACK_ACTION_PAUSE
        | AVPlaybackState.PLAYBACK_ACTION_PLAY_PAUSE
        | AVPlaybackState.PLAYBACK_ACTION_SKIP_TO_NEXT
        | AVPlaybackState.PLAYBACK_ACTION_SKIP_TO_PREVIOUS
        | AVPlaybackState.PLAYBACK_ACTION_STOP
        | AVPlaybackState.PLAYBACK_ACTION_SEEK_TO;
    private final PlayService mPlayService;
    private AVSession mMediaSession;
    private AVSessionCallback callback = new AVSessionCallback() {
        @Override
        public void onPlay() {
            mPlayService.playPause();
        }

        @Override
        public void onPause() {
            mPlayService.playPause();
        }

        @Override
        public void onPlayNext() {
            mPlayService.next();
        }

        @Override
        public void onPlayPrevious() {
            mPlayService.prev();
        }

        @Override
        public void onStop() {
            mPlayService.stop();
        }

        @Override
        public void onSeekTo(long pos) {
            mPlayService.seekTo(pos);
        }
    };

    /**
     * 构造函数
     *
     * @param playService playService
     */
    public MediaSessionManager(PlayService playService) {
        mPlayService = playService;
        setupMediaSession();
    }

    private void setupMediaSession() {
        mMediaSession = new AVSession(mPlayService, TAG);
        mMediaSession.setAVSessionCallback(callback);
        mMediaSession.enableAVSessionActive(true);
    }

    /**
     * 更新播放状态
     */
    public void updatePlaybackState() {
        int state = (mPlayService.isPlaying() || mPlayService.isPreparing()) ? AVPlaybackState.PLAYBACK_STATE_PLAYING
            : AVPlaybackState.PLAYBACK_STATE_PAUSED;
        mMediaSession.setAVPlaybackState(
            new AVPlaybackState.Builder()
                .setAVPlaybackActions(MEDIA_SESSION_ACTIONS)
                .setAVPlaybackState(state, mPlayService.getCurrentPosition(), 1)
                .build());
    }

    /**
     * 更新数据
     *
     * @param music 歌曲
     */
    public void updateMetaData(AudioBean music) {
        if (music == null) {
            mMediaSession.setAVMetadata(null);
            return;
        }

        AVMetadata.Builder metaData = new AVMetadata.Builder()
            .setString(AVMetadata.AVTextKey.TITLE, music.getTitle())
            .setString(AVMetadata.AVTextKey.ARTIST, music.getArtist())
            .setString(AVMetadata.AVTextKey.ALBUM, music.getAlbum())
            .setString(AVMetadata.AVTextKey.ALBUM_ARTIST, music.getArtist())
            .setLong(AVMetadata.AVLongKey.DURATION, music.getDuration());
        metaData.setLong(AVMetadata.AVLongKey.TOTAL_TRACKS,
            BaseAppHelper.get().getMusicList().size());
        mMediaSession.setAVMetadata(metaData.build());
    }

    /**
     * release
     */
    public void release() {
        mMediaSession.setAVSessionCallback(null);
        mMediaSession.enableAVSessionActive(false);
        mMediaSession.release();
    }
}
